package com.kgc.storeprovider.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.store.pojo.OutStore;
import com.kgc.store.pojo.Quality;
import com.kgc.store.pojo.QualityDetail;
import com.kgc.store.pojo.QualityType;
import com.kgc.storeprovider.dao.QualityDetailMapper;
import com.kgc.storeprovider.dao.QualityMapper;
import com.kgc.storeprovider.service.QualityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class QualityServiceImpl implements QualityService {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private QualityMapper qualityMapper;

    @Autowired
    private QualityDetailMapper qualityDetailMapper;
    @Override
    public ResponseMsg getQualityList(Map<String, Object> map) {
        logger.info("QualityServiceImpl getQualityList map:"+map);
        ResponseMsg msg = new ResponseMsg();
        PageInfo<Quality> pageInfo = new PageInfo<>();
        if(map.containsKey("pageNo")){
            pageInfo.setPageNo(Integer.valueOf(map.get("pageNo").toString()));
        }
        if(map.containsKey("pageSize")){
            pageInfo.setPageSize(Integer.valueOf(map.get("pageSize").toString()));
        }
        Page<Object> page = PageHelper.startPage(pageInfo.getPageNo(), pageInfo.getPageSize());
        List<Quality> qualityList = qualityMapper.getQualityList(map);
        logger.debug("QualityServiceImpl getQualityList qualityList:"+qualityList);
        pageInfo.setList(qualityList);
        pageInfo.setPages(page.getPages());
        pageInfo.setTotal((int)page.getTotal());
        msg.setCode("200");
        msg.setData(pageInfo);
        logger.debug("QualityServiceImpl getQualityList msg:"+msg);
        return msg;
    }

    @Override
    public ResponseMsg delQuality(String code) {
        logger.info("QualityServiceImpl delQuality code:"+code);
        ResponseMsg msg = new ResponseMsg();
        int delCount = qualityMapper.delQuality(code);
        qualityDetailMapper.delQualityDetailByQualityCode(code);
        logger.debug("QualityServiceImpl delQuality delCount:"+delCount);
        if(delCount>0){
            msg.setCode("200");
            msg.setData(true);
        }else{
            msg.setCode("201");
            msg.setData(false);
        }
        logger.debug("QualityServiceImpl delQuality msg:"+msg);
        return msg;
    }

    @Override
    public ResponseMsg addQuality(Quality quality) {
        ResponseMsg msg = new ResponseMsg();
        int insertCount = qualityMapper.addQuality(quality);
        if(insertCount>0){
            msg.setData(true);
            msg.setCode("200");
        }else{
            msg.setData(false);
            msg.setCode("201");
        }
        return msg;
    }

    @Override
    public ResponseMsg selectQualityType() {
        logger.info("QualityServiceImpl selectQualityType is start..");
        ResponseMsg msg = new ResponseMsg();
        List<QualityType> qualityTypeList = qualityMapper.selectQualityType();
        logger.debug("QualityServiceImpl selectQualityType qualityTypeList:"+qualityTypeList);
        msg.setCode("200");
        msg.setData(qualityTypeList);
        logger.debug("QualityServiceImpl selectQualityType msg:"+msg);
        return msg;
    }
    @Override
    public ResponseMsg selectQualityDetailByCGDD(String cgddCode) {
        ResponseMsg msg = new ResponseMsg();
        List<QualityDetail> qualityDetailList = qualityMapper.selectQualityDetailByCGDD(cgddCode);
        msg.setCode("200");
        msg.setData(qualityDetailList);
        return msg;
    }

}
